import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { MenuManagerService } from '../../../services/menu-manager.service';

@Component({
  selector: 'app-auth-drawer',
  templateUrl: './auth-drawer.component.html',
  styleUrls: ['./auth-drawer.component.less']
})
export class AuthDrawerComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('configTypeItemModal', { static: false })
  configTypeItemModal!: any;
  columns: STColumn[] = this.initST();
  searchSchema: SFSchema = this.initSF();

  id = null;
  appId = '';
  code = '';

  functionInfo: any = {};
  functions: any[] = [];
  isDisabled = false;

  constructor(public service: MenuManagerService, private modal: NzModalService) {}

  ngOnInit(): void {
    // this.loadFunctions();
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, { id: this.id });
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value
      });
    }
    return requestOptions;
  };

  loadFunctions() {
    this.service.request(this.service.$api_get_functions, { appId: this.appId }, 'POST', false).subscribe(res => {
      if (res) {
        this.functions = res;
      }
    });
  }

  functionAction(item?: any, isDisabled = false) {
    if (item) {
      this.functionInfo = { ...item, id: item.functionButtonId };
    } else {
      this.functionInfo = {};
    }
    this.isDisabled = isDisabled;
    const modal = this.modal.create({
      nzTitle: item ? '编辑' : '新增',
      nzContent: this.configTypeItemModal,
      nzWidth: 800,
      nzCancelText: '取消',
      nzOkText: '保存',
      nzOnOk: () => {
        this.service.request(this.service.$api_save_menu_function, { ...this.functionInfo, functionId: this.id }).subscribe(res => {
          if (res) {
            this.service.msgSrv.success(item ? '编辑成功' : '新增成功');
            this.st.load(1);
            modal.destroy();
          }
        });
        return false;
      }
    });
  }

  deleteAuth(item: any) {
    const modal = this.modal.warning({
      nzTitle: '是否确定删除该权限',
      nzOnOk: () => {
        this.service.request(this.service.$api_delete_menu_function, [item.functionButtonId]).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('删除成功');
            this.st.load(1);
            modal.destroy();
          } else {
            this.service.msgSrv.error('删除失败');
          }
        });
        return false;
      }
    });
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }

  private initSF(): SFSchema {
    return {
      properties: {
        vc2code: {
          type: 'string',
          title: '权限名称',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入权限名称'
          }
        },
        vc2c2ode: {
          type: 'string',
          title: '权限编号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入权限编号'
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '权限名称', index: 'permissionsName', width: 120 },
      { title: '权限编码', render: 'permissionsCode' },
      { title: '权限路径', index: 'permissionsUrl', className: 'break-word-all', width: 200 },
      {
        title: '操作',
        width: '150px',
        className: 'text-center',
        buttons: [
          {
            text: '查看',
            click: item => this.functionAction(item, true)
          },
          {
            text: '编辑',
            click: item => this.functionAction(item)
          },
          {
            text: '删除',
            click: item => this.deleteAuth(item)
          }
        ]
      }
    ];
  }
}
